﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IHISCETeim.Model;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using FineUI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Data;
using System.Drawing;

namespace IHISCETeim.Web.AgentSys.SvrMgr
{
    public partial class AgentQltyList : AgentPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get { return "AgentQltyList"; }
        }

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("SerialNumber", "") != "")
            {
                ttbSearchMessage.Text = GetRequestValue<string>("SerialNumber", "");
            }

            if (!IsPostBack)
            {
                LoadData();
            }

           
        }

        private void LoadData()
        {
            // 权限检查
            CheckPowerWithButton(Permission.新增, btnNew);

            // 每页记录数
            Grid1.PageSize = PageSize;
            ddlGridPageSize.SelectedValue = PageSize.ToString();

            BindGrid();
        }


        protected string GetSQLWhere()
        {
            string SeaVal = ttbSearchMessage.Text.Trim();
            string wheresql = string.Format("UserId={0}",base.User.UserId);
            string sqlcommonFiled = "";
            if (!string.IsNullOrEmpty(SeaVal))
            {
                //sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("ClaimsNumber", QueryOperatorsEnum.包含, SeaVal);
                sqlcommonFiled = string.Format("( ClaimsNumber like '%{0}%' or SerialNumber like '%{0}%' or CustomerName like '%{0}%' )", SeaVal);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and  " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            //if (!string.IsNullOrEmpty(SeaVal))
            //{
            //    sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("SerialNumber", QueryOperatorsEnum.包含, SeaVal);
            //    if (!string.IsNullOrEmpty(wheresql))
            //    {
            //        wheresql += " or " + sqlcommonFiled;
            //    }
            //    else
            //    {
            //        wheresql = sqlcommonFiled;
            //    }
            //}
            //if (!string.IsNullOrEmpty(SeaVal))
            //{
            //    sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("CustomerName", QueryOperatorsEnum.包含, SeaVal);
            //    if (!string.IsNullOrEmpty(wheresql))
            //    {
            //        wheresql += " or " + sqlcommonFiled;
            //    }
            //    else
            //    {
            //        wheresql = sqlcommonFiled;
            //    }
            //}


            return wheresql;
        }

        private void BindGrid()
        {
            Grid1.DataSource = AgentQltyController.GetPagination(GetSQLWhere(), Grid1.PageIndex + 1, Grid1.PageSize, Grid1.SortField + " " + Grid1.SortDirection, ref TotalCount)[0];
            Grid1.RecordCount = TotalCount;
            Grid1.DataBind();
        }

        #endregion

        #region Events

        protected void ttbSearchMessage_Trigger2Click(object sender, EventArgs e)
        {
            ttbSearchMessage.ShowTrigger1 = true;
            Grid1.PageIndex = 0;
            BindGrid();
        }

        protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        {
            ttbSearchMessage.Text = String.Empty;
            ttbSearchMessage.ShowTrigger1 = false;
            Grid1.PageIndex = 0;
            BindGrid();
        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }


        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            int QltyId = GetSelectedDataKeyID(Grid1);

            
            if (e.CommandName == "selectSerialNumber")
            {
                Grid1.PageIndex = 0;
                this.ttbSearchMessage.Text = AgentQltyController.GetByQltyId(QltyId).SerialNumber;
                BindGrid();
            }
        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            // 更改每页显示数目时，防止 PageIndex 越界
            if (Grid1.PageIndex > Grid1.PageCount - 1)
            {
                Grid1.PageIndex = Grid1.PageCount - 1;
            }
            BindGrid();
        }

        #endregion

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            DataRowView dr = e.DataItem as DataRowView;
            try
            {

                System.Web.UI.WebControls.HyperLink execlClaims = Grid1.Rows[e.RowIndex].FindControl("execlClaims") as System.Web.UI.WebControls.HyperLink;
                System.Web.UI.WebControls.Label lbSvrResult = Grid1.Rows[e.RowIndex].FindControl("lbSvrResult") as System.Web.UI.WebControls.Label;
                System.Web.UI.WebControls.Label lbClaimState = Grid1.Rows[e.RowIndex].FindControl("lbClaimState") as System.Web.UI.WebControls.Label;

                lbSvrResult.Text = StringHelper.StripTags(dr["SvrResult"].ToString(), false);

                execlClaims.NavigateUrl = "javascript:openExecl(" + dr["QltyId"] + ")";

                string _ClaimState = dr["ClaimState"].ToString();
                switch (_ClaimState)
                { 
                    case "品情再确认":
                        lbClaimState.Text = string.Format("<a href=\"Javascript:editQlty({0},'{2}')\" title=\"驳回原因:{3}\">{1}</a> [{4}]", dr["QltyId"], _ClaimState, dr["ClaimsNumber"], dr["CancelReason"],"编");
                        break;
                    case "品情已确认":
                        lbClaimState.Text = string.Format("<a href=\"Javascript:editAffiliated({0},'{2}')\" >{1}</a> [{3}]", dr["QltyId"], _ClaimState, dr["ClaimsNumber"],"附");
                        break;
                    default:
                        lbClaimState.Text = _ClaimState;
                        break;
                }
                //if (_ClaimState == ClaimState.品情驳回.ToString())
                //{
                //    lbClaimState.Text = string.Format("<a href=\"Javascript:editQlty({0},'{2}')\" title=\"驳回原因:{3}\">{1}</a>", dr["QltyId"], _ClaimState, dr["ClaimsNumber"], dr["CancelReason"]);
                //}
                //else if (_ClaimState == ClaimState.品情驳回.ToString())
                //{
                    
                //}
                //else {
                //    lbClaimState.Text = _ClaimState;
                //}


                lbClaimState.ForeColor = GetColorByClaimState(_ClaimState);

            }
            catch
            {

            }
        }
    }
}